﻿using ImageProcessor;
using ImageProcessor.Imaging.Formats;
using Sino.CapacityCloud.Repositories.Models;
using Sino.CapacityCloud.WebApi;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Sino.CapacityCloud.Repositories.Models
{

    public static class ApplicationUserExtensions
    {

        public static string GetSmallAvatar(this ApplicationUser user)
        {
            return user.AvatarKey;
        }

        public static async Task<Stream> GetSmallAvatarAsync(this ApplicationUser user, int height = 64, int width = 64)
        {
            try
            {
                var url = user?.AvatarKey;
                if (!string.IsNullOrWhiteSpace(url))
                {
                    using (var client = new HttpClient())
                    {
                        using (var inputStream = await client.GetStreamAsync(url))
                        {
                            using (var outputStream = new MemoryStream())
                            {
                                using (var imageFactory = new ImageFactory())
                                {
                                    // Load, resize, set the format and quality and save an image.
                                    imageFactory
                                        .Load(inputStream)
                                        .Resize(new Size(width, height))
                                        .Format(new JpegFormat())
                                        .Quality(70)
                                        .Save(outputStream);
                                }
                                outputStream.Flush();
                                return outputStream;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
            }
            return null;
        }

        public static RealNameState GetCertificateStatus(this ApplicationUser user)
        {
            if (user != null)
            {
                var states = new CertificationStatus[]
                {
                    user.DrivingLicenseKeyCertificationState,
                    user.CertificatePhotoCertificationState,
                    user.Car?.CertificationStatus ?? CertificationStatus.Uncommitted
                };
                var overallState = RealNameState.Pass;
                if (states[0] == CertificationStatus.Passed &&
                    states[1] == CertificationStatus.Passed &&
                    states[2] == CertificationStatus.Passed)
                {
                    overallState = RealNameState.Pass;
                }
                else
                {
                    while (true)
                    {
                        if (states[0] == CertificationStatus.Rejected ||
                            states[1] == CertificationStatus.Rejected ||
                            states[2] == CertificationStatus.Rejected)
                        {
                            overallState = RealNameState.Failed;
                            break;
                        }
                        if (states[0] == CertificationStatus.Pending ||
                            states[1] == CertificationStatus.Pending ||
                            states[2] == CertificationStatus.Pending)
                        {
                            overallState = RealNameState.Pending;
                            break;
                        }
                        else
                        {
                            overallState = RealNameState.Uncommitted;
                            break;
                        }
                    }
                }
                return overallState;
            }
            return RealNameState.None;
        }

        public static bool IsCertificated(this ApplicationUser user)
        {
            try
            {
                if (user != null)
                {
                    if (user.DrivingLicenseKeyCertificationState == CertificationStatus.Passed &&
                        user.CertificatePhotoCertificationState == CertificationStatus.Passed &&
                        user.Car?.CertificationStatus == CertificationStatus.Passed)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static RealNameInfo GetRealNameInfo(this ApplicationUser user)
        {
            try
            {
                var info = new RealNameInfo()
                {
                    State = user.GetCertificateStatus(),
                    CarCode = user?.Car?.Code,
                    RealName = user.RealName,
                    IDCardState = (RealNameState)user.IdentityCardCertificationState,
                    IDCardImage = user.IdentityCardKey,
                    DrivingLicenseState = (RealNameState)user.DrivingLicenseKeyCertificationState,
                    DrivingLicenseImage = user.DrivingLicenseKey,
                    VehicleLicenseImage = user.Car?.VehicleLicenseKey,
                    VehicleLicenseState = (RealNameState)(user.Car?.CertificationStatus ?? CertificationStatus.Uncommitted),
                    TrailerLicenseImage = user.Car?.TrailerLicenseKey,
                    TrailerLicenseState = (RealNameState)(user.Car?.TrailerLicenseCertificationStatus ?? CertificationStatus.Uncommitted),
                    VehiclePhotoImage = user.Car?.VehiclePhotoKey,
                    VehiclePhotoState = (RealNameState)(user.Car?.VehiclePhotoCertificationStatus ?? CertificationStatus.Uncommitted),
                    RealHeadState = (RealNameState)user.CertificatePhotoCertificationState,
                    RealHeadImage = user.CertificatePhotoKey,
                    Message = ""
                };
                return info;
            }
            catch (Exception)
            {
                return null;
            }
        }

    }

}